#!/usr/bin/env python2.7
# -*- coding:utf-8 -*- 

# china_9km_anl2.py

import os, sys
from metlib.kits import *
from datakeeper.models import DataSet, DatasetMold

ds_name = 'c9a2_solar'
mold_name = 'WRF'
ds_desc = u'大贤的全国9km再分析数据集第2版(太阳能)'


def run():
    try:
        raise DataSet.DoesNotExist
        ds = DataSet.objects.get(name=ds_name)
        print "%s exists, not creating." % ds_name
    except DataSet.DoesNotExist:
        try:
            mold = DatasetMold.objects.get(name=mold_name)
        except DatasetMold.DoesNotExist:
            print "No such dataset-mold: " + mold_name
            sys.exit(1)
        print "Creating dataset: %s ..." % ds_name
        ds = DataSet(
            name=ds_name,
            desc=ds_desc,
            creator='dxwind',
            version='1.0',
            mold=mold,
            priority=20,
            protect_data=True,
        )
        ds.info = {
            "name": "c9a2_solar",
            "zh_name": u"9km再分析-太阳能(C9A2 solar)",
            "desc": ds_desc,
            "version": 2,
            "WRF_version": "",
            "begdt": "2015-01-01 00",
            "enddt": "2016-01-01 00",
            "years": ["2015"],
            "levels": ['10'],
            "sfc_vars": ['rain', 'snow', 'swdown', 'glw', 'ts', 'slp'],
            "py_varnames": ['sfc_summary'],
            "pm_varnames": ['swdown', 'glw', 'rain', 'snow', 'td', 'rh', 'psfc'],
            "pd_varnames": ['swdown', 'glw', 'rain', 'snow', 'td', 'rh', 'psfc'],
            "pt_varnames": ['swdown', 'glw', 'rain', 'snow', 'td', 'rh', 'psfc'],
            "ry_varnames": ['swdown', 'glw', 'rain', 'snow', 'td', 'rh', 'psfc'],
            # "py_summary_sub_varnames": ['td', 'psfc', 'rhoair', 'rh'],
            "py_sfc_summary_sub_varnames": ['swdown', 'glw', 'ts', 'rain', 'snow'],
            "py_stat_format": "nc",
            "pm_stat_format": "nc",
            "pd_stat_format": "nc",
            "py_rose_format": "nc",
            "py_wpdrose_format": "nc",
            "py_dist_format": "nc",
            "pt_format": "json",
            "dataset_storage_name": "china_9km_anl2",
            "domain_info": {"1": {"available": True, "priority": 7, "trim": 0},
                            "2": {"available": True, "priority": 3, "trim_left": 5, "trim_right": 4, "trim_top": 6, "trim_bottom": 0},
                            "3": {"available": True, "priority": 4, "trim_left": 4, "trim_right": 5, "trim_top": 6, "trim_bottom": 4},
                            "4": {"available": True, "priority": 1, "trim_left": 0, "trim_right": 0, "trim_top": 0, "trim_bottom": 3},
                            "5": {"available": True, "priority": 0, "trim_left": 6, "trim_right": 4, "trim_top": 2, "trim_bottom": 10},
                            "6": {"available": True, "priority": 6, "trim_left": 4, "trim_right": 4, "trim_top": 0, "trim_bottom": 4},
                            "7": {"available": True, "priority": 5, "trim_left": 4, "trim_right": 0, "trim_top": 0, "trim_bottom": 4},
                            "8": {"available": True, "priority": 2, "trim_left": 3, "trim_right": 7, "trim_top": 22, "trim_bottom": 6}},
            "wps_namelist": """
&share
 max_dom    = 8,
/

&geogrid
 parent_id         =   1,     1,      1,      1,      1,      1,      1,      1,
 parent_grid_ratio =   1,     3,      3,      3,      3,      3,      3,      3,
 i_parent_start    =   1,     135,    84,     6,      76,     140,    150,    20,
 j_parent_start    =   1,     6,      15,     76,     60,     60,     110,    40,
 e_we              =   240,   196,    199,    253,    223,    187,    253,    232,
 e_sn              =   186,   193,    166,    232,    181,    187,    208,    142,
 dx                =   27000,
 dy                =   27000,
 map_proj          =   'lambert',
 ref_lat           =   38,
 ref_lon           =   104,
 truelat1          =   5.0,
 truelat2          =   10.0,
 stand_lon         =   104,
/
            """
        }

        try:
            ds.save(update_fields=['desc', 'creator', 'version', 'mold', 'priority', 'protect_data', 'info_json'])
        except Exception:
            ds.save()
